<template>
  <div class="sidebar">
    <div class="profile">
      <!-- 整合个人中心的头像上传功能 -->
      <div class="avatar-container">
        <el-upload
          class="avatar-uploader"
          :action="define.comUploadUrl + '/userAvatar'"
          :headers="uploadHeaders"
          :on-success="handleAvatarUpload"
          :show-file-list="false"
          accept="image/*"
        >
        <div class="avatar-box">
            <el-avatar :size="50" :src="define.comUrl + user.avatar" class="avatar"
              v-if="user.avatar" />
            <div class="avatar-hover">更换头像</div>
          </div>
        </el-upload>
      </div>
      <div class="info">
        <h2 class="username">{{ user.realName || '芒果' }}</h2>
        <p class="motto">{{ user.motto || '个签：冲击算法岗！' }}</p>
      </div>
    </div>
    <div class="menu">
      <button
        class="menu-btn"
        @click="handleClick('community')"
        :class="{ active: activeMenu === 'community' }"
      >进入 Mango 社区</button>
      <button
        class="menu-btn"
        @click="handleClick('resume-edit')"
        :class="{ active: activeMenu === 'resume-edit' }"
      >智能简历生成/润色</button>
      <button
        class="menu-btn"
        @click="handleClick('resume-analyze')"
        :class="{ active: activeMenu === 'resume-analyze' }"
      >简历解析</button>
    </div>
    <div class="task-recommend">
      <h3>今日推荐任务卡片区</h3>
      <ul>
        <li @click="handleTask('mock-interview')">[进入一次模拟面试]</li>
        <li @click="handleTask('feedback-report')">[查看一次反馈报告]</li>
        <li @click="handleTask('high-frequency-question')">[练习一道高频题]</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { UserSettingInfo, UpdateAvatar } from '@/api/permission/userSetting'

export default {
  name: 'Sidebar',
  data() {
    return {
      user: {
        realName: '芒果',
        avatar: '',
        motto: '冲击算法岗！'
      },
      activeMenu: 'community',
      uploadHeaders: {
        Authorization: `Bearer ${localStorage.getItem('token') || ''}`
      }
    }
  },
  computed: {
    avatarUrl() {
      return this.define.comUrl + this.user.avatar
    }
  },
  created() {
    this.fetchUserInfo()
  },
  methods: {
    // 获取用户信息（整合个人中心逻辑）
    fetchUserInfo() {
      UserSettingInfo().then(res => {
        this.user = res.data || this.user
      }).catch(err => {
        console.error('获取用户信息失败', err)
      })
    },
    // 处理头像上传（整合个人中心逻辑）
    handleAvatarUpload(res, file) {
      if (res.code === 200) {
        this.updateAvatar(res.data)
      } else {
        this.$message({
          message: '上传失败',
          type: 'error',
          duration: 1000
        })
      }
    },
    // 更新头像（整合个人中心逻辑）
    updateAvatar(data) {
      UpdateAvatar(data.name).then(res => {
        this.user.avatar = data.url
        this.$store.commit('user/SET_AVATAR', data.url)
        this.$message({
          message: res.msg || '头像更新成功',
          type: 'success',
          duration: 1000
        })
      }).catch(err => {
        console.error('更新头像失败', err)
      })
    },
    // 菜单点击处理
    handleClick(type) {
      this.activeMenu = type
      console.log(`点击了 ${type} 功能`)
      // 可添加路由跳转等逻辑
    },
    // 任务点击处理
    handleTask(taskType) {
      console.log(`处理 ${taskType} 任务`)
    }
  }
}
</script>

<style scoped>
.sidebar {
  width: 220px;
  background-color: #f8f9fa;
  padding: 20px;
  box-sizing: border-box;
}
.profile {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}
.avatar-container {
  position: relative;
}
.avatar-box {
  position: relative;
}
.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 10px;
}
.avatar-hover {
  position: absolute;
  left: 0;
  top: 0;
  font-size: 12px;
  display: none;
  width: 50px;
  height: 50px;
  text-align: center;
  border-radius: 50%;
  line-height: 50px;
  color: #fff;
  cursor: pointer;
  background: rgba(0, 0, 0, 0.5);
}
.avatar-uploader:hover .avatar-hover {
  display: block;
}
.info .username {
  margin: 0;
  font-size: 16px;
}
.info .motto {
  margin: 0;
  font-size: 12px;
  color: #6c757d;
}
.menu {
  margin-bottom: 20px;
}
.menu-btn {
  width: 100%;
  text-align: left;
  padding: 10px;
  margin-bottom: 8px;
  background-color: #fff;
  border: 1px solid #dee2e6;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
}
.menu-btn.active {
  background-color: #e9ecef;
  border-color: #b3d8ff;
  color: #409EFF;
}
.menu-btn:hover {
  background-color: #e9ecef;
}
.task-recommend h3 {
  margin-bottom: 10px;
  font-size: 14px;
}
.task-recommend ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.task-recommend li {
  padding: 8px 0;
  cursor: pointer;
  color: #007bff;
  transition: color 0.3s ease;
}
.task-recommend li:hover {
  color: #0056b3;
}
</style>
